package testMaven;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class ServiceInstanceRestController {
	
    private static final Logger logger = LoggerFactory.getLogger(ServiceInstanceRestController.class);
    
    @Autowired
    private DiscoveryClient discoveryClient; //服务发现客户端

    @GetMapping(value = "/add")
    public Integer add(HttpServletRequest request,@RequestParam Integer a, @RequestParam Integer b) {
    	System.out.println(request.getSession().getId());
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        Integer r = a + b;
        logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
        return r;
    }
    
    @GetMapping(value = "/toJsp")
    public ModelAndView toJsp(HttpServletRequest request) {
        return new ModelAndView("index");
    }
    
    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        System.out.println(name+" welcome . My is microservice provider user");
        return name+" welcome . My is microservice provider user";
    }
    
    @RequestMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello";
    }
}
